/* Printer status information. */
/* Configuration handling */
/* Created 22.11.2005 T. Milius
   Changed 22.11.2005 T. Milius */
/* (c) Copyright 2005 by Thomas Milius Stade, Germany
   Source must not be altered without agreement of the owner.
   The owner of the source is allowed to use this code inside programs without
   publishing the code of this programs. These programs may be commercial.
   Other developers can use this source freely inside own software if the source
   code of this programs is made public to same conditions like valid to this code
   and no commercial profit is taken from the programs based on this code

   Code or parts of it are not allowed to be used within GPL code or
   similar licenses which are "infecting" other code and trying to "supersede"
   other licenses. */
/* ANSI-C */

#ifndef settings_h
#define settings_h

/* !!!!!!!!!! libraries !!!!!!!!!! */
#include <ctype.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>

/* !!!!!!!!!!! definitions !!!!!!!!!! */
#define SETTING_TAG_NETWORK          "Network:"
#define SETTING_TAG_NETMASK          "Netmask:"
#define SETTING_TAG_PORT             "Port:"
#define SETTING_TAG_NETWORKTRACEFILE "NetworkTraceFile:"

/* !!!!!!!!!! data structures !!!!!!!!!! */
struct settings_struct {
char network[40];
char netmask[40];
int port;
char trace_file_name[100];
};

/* !!!!!!!!!! support functions !!!!!!!!!! */

/* !!!!!!!!!! functions !!!!!!!!!! */
bool read_settings(struct settings_struct *settings)
{
char *c;
char input_line[MAX_STRING_LENGTH];
FILE *choices_file;

if (!settings) return false;
strcpy(settings->network, "0.0.0.0");
strcpy(settings->netmask, "255.255.255.255");
settings->port=0;
strcpy(settings->trace_file_name, "");
if ((choices_file=fopen("Choices:PrtInfo.settings", "r")) == NULL) return false;
input_line[MAX_STRING_LENGTH - 1]='\0';
while ((!feof(choices_file))) {
  strcpy(input_line, "");
  fgets(input_line, MAX_STRING_LENGTH - 1, choices_file);
  if (!feof(choices_file)) {
    c=&input_line[strlen(input_line) - 1];
    while ((c >= input_line) &&
           ((*c == '\n') ||
            (isspace(*c)))) {
      *c='\0';
      c--;
      }
    if (input_line[0] != '#') {
      if (strncmp(input_line, SETTING_TAG_NETWORK, strlen(SETTING_TAG_NETWORK)) == 0) {
        sscanf(input_line,
               "%*s %s",
               settings->network);
        }
      else if (strncmp(input_line, SETTING_TAG_NETMASK, strlen(SETTING_TAG_NETMASK)) == 0) {
        sscanf(input_line,
               "%*s %s",
               settings->netmask);
        }
      else if (strncmp(input_line, SETTING_TAG_PORT, strlen(SETTING_TAG_PORT)) == 0) {
        sscanf(input_line,
               "%*s %d",
               &settings->port);
        }
      else if (strncmp(input_line, SETTING_TAG_NETWORKTRACEFILE, strlen(SETTING_TAG_NETWORKTRACEFILE)) == 0) {
        sscanf(input_line,
               "%*s %s",
               settings->trace_file_name);
        }
      }
    }
  }
fclose(choices_file);
return true;
}

#endif
